package View;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.JScrollPane;
import javax.swing.JTable;

import Controller.DBLink;
import Model.ListTableModel;

import javax.swing.SwingConstants;

public class RegisterWindow extends JFrame{
	
	public JPanel panel;
	public String sql_table;
	public ListTableModel mod;
	public ArrayList<JTextField> text;
	public ArrayList<JLabel> text_label;
	public MainWindow my_main;
	public DBLink dblink;
	
	public RegisterWindow(DBLink db,MainWindow mai){
		// TODO Auto-generated constructor stub
		setTitle("Registrar");
		setBounds(50,50,320,420);
		//setMinimumSize(new Dimension(320,420));
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.dblink = db;
		System.out.println("DB: " + (dblink==null));
		
		panel = new JPanel();
		panel.setBounds(0, 0, 320, 420);
		panel.setLayout(null);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnArquivo = new JMenu("Arquivo");
		menuBar.add(mnArquivo);
		
		JMenuItem mntmSair = new JMenuItem("Sair");
		final JFrame f = this;
		mntmSair.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
		mntmSair.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("MFechando Tudo!!!");
				Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new WindowEvent(f, WindowEvent.WINDOW_CLOSING));
			}
		});
		mnArquivo.add(mntmSair);
		getContentPane().setLayout(null);
		getContentPane().add(panel);
		
		my_main = mai;
		
		text = new ArrayList<JTextField>();
		text_label = new ArrayList<JLabel>();
	}

	public String fix(String c){
		int f = 0;
		String s = "";
		for(int i = 0 ;i < c.length(); i++){
			if(c.charAt(i) == ' '){f++; s += '_';}
			else s+= c.charAt(i);
		}
		//System.out.println("Score: " + f + " " + s);
		return s;
	}
	
	public void suitUp(ListTableModel m, String sql){
		mod = m;
		sql_table = "" + getTable(sql);
		System.out.println("TableName: " + sql_table);
		panel.removeAll();
		text.clear();
		text_label.clear();
		for(int i = 0;i < mod.getColumnCount();i++){
			text.add(new JTextField());
			String col = mod.getColumnName(i);
			col = fix(col);
			text_label.add(new JLabel(col + ": "));
			text_label.get(i).setHorizontalAlignment(SwingConstants.RIGHT);
			text_label.get(i).setBounds(10, 20 + i*25, 90, 20);
			
			text.get(i).setBounds(100, 20 + (i*25), 150, 20);
			panel.add(text.get(i));
			panel.add(text_label.get(i));
		}
		
		JButton can = new JButton("Cancel");
		can.setBounds(150, 320, 90, 24);
		can.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("Apertei Cancel!!!");
				setVisible(false);
			}
		});
		panel.add(can);
		
		JButton ook = new JButton("Registrar");
		ook.setBounds(50, 320, 90, 24);
		ook.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("Apertei Ok!!!");
				makeRegistry();
				my_main.executeRefresh();
			}
		});
		panel.add(ook);
		
		setVisible(true);
		repaint();
	}
	
	public String makeColumns(){
		String ret = "";
		
		for(int i = 0;i < text_label.size();i++){
			String t = text_label.get(i).getText().substring(0, text_label.get(i).getText().length()-2);
			ret += t;
			if(i != text_label.size()-1)ret += ',';
		}
		ret += ") values ";
		
		return ret;
	}
	
	public String makePass(){
		String sql = "select cadastrapassageiro(";
		for(int i = 0;i < text.size();i++){
			String t = text.get(i).getText(); 
			if(!t.isEmpty())sql += "'" + t + "'";
			else sql+= "null";			
			if(i != text.size()-1)sql += ',';
		}
		sql += ")";
		return sql;
	}
	
	public String makeValues(){
		String values = " (";
		for(int i = 0;i < text.size();i++){
			String t = text.get(i).getText(); 
			if(!t.isEmpty())values += "'" + t + "'";
			else values+= "null";			
			if(i != text.size()-1)values += ',';
		}
		values += ") ";
		return values;
	}
	
	public void makeRegistry(){
		String sql = "";
		if(sql_table.equalsIgnoreCase("passageiro")){
			sql += makePass();
			if(dblink.runSQLcmd(sql))this.setVisible(false);
			else this.setVisible(true);
		}
		else{
			sql = "insert into " + sql_table + " (";
			sql += makeColumns();
			sql += makeValues();
			if(dblink.runSQLcmd(sql))this.setVisible(false);
			else this.setVisible(true);
		}
		
		System.out.println("Registry: " + sql);
	}
	
	public String getTable(String sql){
		String s = "";
		String ss = "";
		for(int i = sql.length()-1;i >= 0;i--){
			if(sql.charAt(i) == ' ') break;
			else ss += sql.charAt(i); 
		}
		for(int i = ss.length()-1;i >= 0;i--){
			s += ss.charAt(i);
		}
		
		return s;
	}
}
